<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/16
 * Time: 12:23
 */

namespace Home\Controller;


use Api\Controller\WxPayController;

class ApplyRefundController extends CommonController
{
    //@todo 加载退款订单页面
    public function index(){
        $this->display();
    }

    //@todo 获取退款订单信息
    public function getOrdersList(){
        $postData=$_GET;
        $where=array();
        if(!empty($postData['orders_no'])){
            $where['orders_no']=$postData['orders_no'];
        }
        if($postData['status'] != ''){
            $where['status']=$postData['status'];
        }else{
            $where['status']=array('IN',array('6','7'));
        }

        $count=D('orders_list')->where($where)->count();
        $list=D('orders_list')->field("*,FROM_UNIXTIME(ac_time) as ac_time")->limit($_GET['offset'],$_GET['limit'])->where($where)->select();
        foreach($list as $key => $value){
            $user_info=D('client_user_address')->where(array('user_id'=>$value['user_id'],'is_code'=>'1'))->find();
            $list[$key]['user_info']=$user_info['name'];
        }
        $this->ajaxReturn(array('rows'=>$list,'total'=>$count));
    }

    //@todo 退款操作
    public function ApplyRefundAction(){
        $money=$_POST['apply_money'];
        $id=$_POST['id'];
        $pay=new  WxPayController();
        $result=$pay->ApplyRefundAction($id,$money);
        if($result['return_code']=='SUCCESS'){
            //退款成功
            $refund=D('orders_list')->save(array('id'=>$id,'status'=>'7','refund_money'=>$money,'refund_time'=>time()));
            if(!is_numeric($refund)){
                $this->ajaxReturn(array('success'=>false,'message'=>'微信商户已退款,但数据库操作异常！'));
            }
            $this->ajaxReturn(array('success'=>true,'message'=>'退款成功'));
        }else{
            $message='';
            switch($result['return_msg']){
                case 'SYSTEMERROR' : $message='系统超时等';break;
                case 'ERROR' : $message='订单可退款时间超限（一年内支付订单可退款）等业务错误';break;
                case 'USER_ACCOUNT_ABNORMAL	' : $message='用户帐号注销';break;
                case 'NOTENOUGH' : $message='商户可用退款余额不足';break;
                case 'INVALID_TRANSACTIONID' : $message='请求参数未按指引进行填写';break;
                case 'PARAM_ERROR' : $message='请求参数未按指引进行填写';break;
                case 'APPID_NOT_EXIST' : $message='参数中缺少APPID';break;
                case 'MCHID_NOT_EXIST' : $message='参数中缺少MCHID';break;
                case 'APPID_MCHID_NOT_MATCH' : $message='appid和mch_id不匹配';break;
                case 'REQUIRE_POST_METHOD' : $message='未使用post传递参数 ';break;
                case 'SIGNERROR' : $message='参数签名结果不正确';break;
                case 'XML_FORMAT_ERROR' : $message='XML格式错误';break;
                case 'FREQUENCY_LIMITED' : $message='2个月之前的订单申请退款有频率限制';break;
                default: $message='未知的错误';break;
            }
            $this->ajaxReturn(array('success'=>false,'message'=>$message));
        }
    }

    //@todo 删除退款订单
    public function delRefund_orders(){
        if(isset($_POST['id'])){
            $id=$_POST['id'];
            $delOrdersList=D('orders_goods_list')->where(array('orders_id'=>$id))->delete();
            if(!$delOrdersList){
                $this->ajaxReturn(array('success'=>false,'message'=>'删除失败'));
            }
            $del=D('orders_list')->delete($id);
            if(!$del){
                $this->ajaxReturn(array('success'=>false,'message'=>'删除失败'));
            }
            $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
        }
        $this->ajaxReturn(array('success'=>false,'message'=>'异常访问'));


    }


    //@todo 查看详情页
    public function detailOrder(){
        if(!isset($_GET['id'])){
            $this->error('非法访问');
        }
        $id=$_GET['id'];
        $list=D('orders_list')->field("*,FROM_UNIXTIME(ac_time) as ac_time,FROM_UNIXTIME(refund_time) as refund_time")->where(array('id'=>$id))->find();
        $user_info=D('client_user_address')->where(array('user_id'=>$list['user_id'],'is_code'=>'1'))->find();
        $list['user_name']=$user_info['name'];
        $list['user_phone']=$user_info['phone'];
        $list['user_address']=$user_info['address'];
        $list['order_xk']='未选择';
        //查询相框尺寸
        if($list['xk_id']!=''&&$list['xk_id']!=null){
            $xk_info=D('photo_box')->find($list['xk_id']);
            $list['order_xk']=$xk_info['text'];
        }
        //查询相册尺寸
        $list['order_xc']='未选择';
        if($list['xc_id']!=''&&$list['xc_id']!=null){
            $xc_info=D('photo_box')->find($list['xc_id']);
            $list['order_xc']=$xc_info['text'];
        }
        $this->assign('info',$list);
        $this->display();
    }

}